package com.game.football;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * 游戏面板类。
 * <p>
 * 该类负责管理游戏的主要逻辑和绘图。
 */
public class FootballPanel extends JPanel implements Runnable {
    private final Ball ball;
    private final Player player;
    private final Goal goal;
    private Thread gameThread;
    private boolean isRunning;

    /**
     * 构造一个新的游戏面板对象。
     */
    public FootballPanel() {
        setPreferredSize(new Dimension(Constants.WIDTH, Constants.HEIGHT));
        setBackground(Color.BLACK);

        ball = new Ball(400, 300, Constants.BALL_SIZE);
        player = new Player(400, 500, Constants.PLAYER_SIZE);
        goal = new Goal(350, 50, Constants.GOAL_WIDTH, Constants.GOAL_HEIGHT);

        // 添加键盘监听器
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                switch (keyCode) {
                    case KeyEvent.VK_LEFT:
                        player.move(-5, 0);
                        break;
                    case KeyEvent.VK_RIGHT:
                        player.move(5, 0);
                        break;
                    case KeyEvent.VK_UP:
                        player.move(0, -5);
                        break;
                    case KeyEvent.VK_DOWN:
                        player.move(0, 5);
                        break;
                    case KeyEvent.VK_SPACE:
                        if (player.getBounds().intersects(ball.getBounds())) {
                            ball.setSpeed(5, -5); // 向上踢球
                        }
                        break;
                }
            }
        });
        setFocusable(true);

        startGame();
    }

    /**
     * 开始游戏。
     */
    private void startGame() {
        isRunning = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    /**
     * 游戏主循环。
     */
    @Override
    public void run() {
        while (isRunning) {
            ball.move();

            // 检查球是否进入目标
            if (ball.getBounds().intersects(goal.getBounds())) {
                System.out.println("进球了！");
                ball.setSpeed(0, 0);
//                ball.x = 400;
//                ball.y = 300;
                ball.setX(400);
                ball.setY(300);
            }

            // 检查球是否出界
            if (ball.getY() > Constants.HEIGHT) {
                ball.setSpeed(0, 0);
                ball.setX(400);
                ball.setY(300);
            }

            repaint();
            try {
                Thread.sleep(16); // 大约每秒60帧
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 重写 paintComponent 方法以绘制游戏元素。
     *
     * @param g 用于绘制的 Graphics 对象
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ball.draw(g);
        player.draw(g);
        goal.draw(g);
    }
}